// https://nuxt.com/docs/api/configuration/nuxt-config
import Components from 'unplugin-vue-components/vite'
import { DevUiResolver } from 'unplugin-vue-components/resolvers'
import CompressionPlugin from 'compression-webpack-plugin'
import dayjs from 'dayjs'
const env = import.meta.env.VITE_ENV
const buildAssetsDir = env === 'production' ? '/_nuxt_news/' : `/_nuxt_news_${env === 'test' ? 'test' : (env || '')}/`
const inDev = ['loc', 'development'].includes(env)

export default defineNuxtConfig({
  modules: [
    '@nuxtjs/tailwindcss',
    'vue-devui',
    [
      '@pinia/nuxt',
      {
        autoImports: [
          // 自动引入 `defineStore()`
          'defineStore',
          // 自动引入 `defineStore()` 并重命名为 `definePiniaStore()`
          ['defineStore', 'definePiniaStore'],
        ],
      },
    ],
    '@vueuse/nuxt',
    'nuxt-lodash', // "nuxt3-winston-log",
    'dayjs-nuxt',
    '@nuxt/image',
  ],

  extends: [
    './node_modules/devui-theme/index.es.js',
    './node_modules/crypto-js/index.js',
  ],
  plugins: [
    {
      src: '~/plugins/read-file.ts',
    },
    {
      src: '~/plugins/router.client.ts',
      ssr: false,
    },
    {
      src: '~/plugins/i18n.ts'
    }
  ],
  pinia: {
    storesDirs: ['./stores/**'],
  },
  devServer: {
    host: 'local.gitcode.net',
    port: 443,
    https: {
      key: './ssl/server.key',
      cert: './ssl/server.crt',
    },
  },
  sourcemap: {
    server: true,
    client: inDev,
  },
  devtools: {
    enabled: inDev,
  },
  vite: {
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "devui-theme/styles-var/devui-var.scss";@import "~/assets/css/mixins/variables.scss";`,
        },
      },
    },
    plugins: [
      // devui组件库按需引入
      Components({
        resolvers: [
          DevUiResolver({
            directives: false,
          }),
        ],
      }),
    ],
  },

  css: [
    '~/assets/css/variable.css',
    '~/assets/css/main.scss',
    '~/assets/css/devui_variable.css',
    'vue-devui/style.css',
    '@devui-design/icons/icomoon/devui-icon.css',
  ],

  tailwindcss: {
    configPath: './tailwind.config.js',
    exposeConfig: { level: 2 },
    config: {},
    viewer: false,
  },
  build: {
    optimization: {
      splitChunks: {
        // 生成 chunk 的最小体积（以 bytes 为单位）
        minSize: 30000,
        // https://webpack.docschina.org/plugins/split-chunks-plugin/
        maxSize: 250000,
      },
    },
    plugins: [
      new CompressionPlugin({
        test: /\.js$|\.html$|\.css/, // 匹配文件名
        threshold: 10240, // 对超过10kb的数据进行压缩
        deleteOriginalAssets: false, // 是否删除原文件
      }),
    ],
  },
  app: {
    head: {
      meta: [
        { charset: 'utf-8' },
        {
          name: 'viewport',
          content: 'width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no',
        },
        {
          name: 'applicable-device',
          content: 'pc,mobile',
        },
        { name: 'referrer', content: 'no-referrer-when-downgrade' },
      ],
      link: [
        { rel: 'icon', href: 'https://cdn-static.gitcode.com/static/images/logo-favicon.png' },
        { rel: 'dns-prefetch', href: 'https://res.hc-cdn.com' },
        { rel: 'dns-prefetch', href: 'https://cdn-static.gitcode.com' },
      ],
      script: [
        {
          src: 'https://cdn-static.gitcode.com/js/font_4205092_a32iu469vv5.js',
          defer: true,
        }
      ],
      style: [
        {
          // 隐藏的编译时间
          children:`/* ${dayjs().format('YYMMDD.HHmmss')} */\n`+
            '.icon { width: 1em; height: 1em;vertical-align: -0.15em; fill: currentColor; overflow: hidden; }',
        },
      ],
    },
    cdnURL: import.meta.env.VITE_STATIC_HOST,
    buildAssetsDir,
    pageTransition: false,
  },
  experimental: {
    inlineSSRStyles: false, // 禁止内联CSS
  },
  compatibilityDate: '2024-07-11',
})